package com.gl.emms.whale.ui;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.KeyEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

import com.gl.emms.whale.R;
import com.gl.emms.whale.app.WhaleMonitorActivity;

public class AccountInfoActivity extends WhaleMonitorActivity implements
OnClickListener{
	private String imagePath;
	
	public static final int REQUEST_CODE_GETIMAGE_BYSDCARD = 2;
	public static final int REQUEST_CODE_GETIMAGE_BYCAMERA = 3;
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏
		setContentView(R.layout.activity_accountinfo);
		Button titleBackBtn = (Button)findViewById(R.id.TitleBackBtn);  
		titleBackBtn.setOnClickListener(new OnClickListener() {  
			public void onClick(View v) {  
				KeyEvent newEvent = new KeyEvent(KeyEvent.ACTION_DOWN,  
						KeyEvent.KEYCODE_BACK);
				finish();
				//mActivity.onKeyDown(KeyEvent.KEYCODE_BACK, newEvent);  
			}  
		});
	}
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

	public void change_avatar(View v) {
		CharSequence[] items = { "手机相册", "手机拍照" };
		imageChooseItem(items);
	}
	public void imageChooseItem(CharSequence[] items) {
		AlertDialog imageDialog = new AlertDialog.Builder(this)
		.setTitle("设置头像")
		.setIcon(android.R.drawable.btn_star)
		.setItems(items, new DialogInterface.OnClickListener() {
			public void onClick(DialogInterface dialog, int item) {
				// 手机选图
				if (item == 0) {
					Intent intent = new Intent(
							Intent.ACTION_GET_CONTENT);
					intent.addCategory(Intent.CATEGORY_OPENABLE);
					intent.setType("image/*");
					startActivityForResult(
							Intent.createChooser(intent, "选择图片"),
							REQUEST_CODE_GETIMAGE_BYSDCARD);
				}
				// 拍照
				else if (item == 1) {
					String savePath = "";
					// 判断是否挂载了SD卡
					String storageState = Environment
							.getExternalStorageState();
					if (storageState.equals(Environment.MEDIA_MOUNTED)) {
						savePath = Environment
								.getExternalStorageDirectory()
								.getAbsolutePath()
								+ "/herotculb/Camera/";// 存放照片的文件夹
						File savedir = new File(savePath);
						if (!savedir.exists()) {
							savedir.mkdirs();
						}
					}
/*
					// 没有挂载SD卡，无法保存文件
					if (KFUtils.isEmpty(savePath)) {
						Toast.makeText(AccountInfoActivity.this,
								"无法保存照片，请检查SD卡是否挂载", Toast.LENGTH_SHORT)
								.show();
						return;
					}
*/
					String timeStamp = new SimpleDateFormat(
							"yyyyMMddHHmmss").format(new Date());
					String fileName = "appkefu_" + timeStamp + ".jpg";// 照片命名
					File out = new File(savePath, fileName);
					Uri uri = Uri.fromFile(out);

					imagePath = savePath + fileName;// 该照片的绝对路径

					Intent intent = new Intent(
							MediaStore.ACTION_IMAGE_CAPTURE);
					intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
					startActivityForResult(intent,
							REQUEST_CODE_GETIMAGE_BYCAMERA);
				}
			}
		}).create();

		imageDialog.show();
	}
}
